<template>
  <div>
    <el-upload
      class="upload-demo"
      :action="uploadImgUrl"
      :headers="headers"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :file-list="imagesFiles"
      list-type="picture"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">
        只能上传jpg/png文件，且不超过5M
      </div>
    </el-upload>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
export default {
  props: {
    uploadUrl: { type: String, default: "" },
    imagesFiles: { type: Array, default: [] },
  },
  data() {
    return {
      headers: { Authorization: "Bearer " + getToken() },
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      fileList: [
        {
          name: "food.jpeg",
          url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
        },
        {
          name: "food2.jpeg",
          url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
        },
      ],
    };
  },
  methods: {
    handleRemove(file) {
      this.imagesFiles.some((item, i) => {
        if (item.uid == file.uid) {
          this.imagesFiles.splice(i, 1);
          return true;
        }
      });
    },
    handlePreview(file) {
      console.log(file);
    },
    handleUploadSuccess(file) {
      this.imagesFiles.push({ name: file.uploadName, url: file.url,fileName: file.uploadName, });
      this.$emit("func", this.imagesFiles);
    },
  },
};
</script>